package com.zfy.cmsservice.controller;


import com.zfy.cmsservice.entity.Courses;
import com.zfy.cmsservice.entity.Exams;
import com.zfy.cmsservice.service.ExamsService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考试表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-12-09
 */
@RestController
@Api(description = "考试管理-考试信息维护")
@RequestMapping("/cmsservice/exams")
public class ExamsController {
    @Autowired
    private ExamsService examsService;

    // 分页查询考试信息 根据考试时间检索 传入开始时间和结束时间
    @ApiOperation(value = "分页查询考试信息")
    @PostMapping("pageExams/{page}/{size}")
    public R pageExams(@PathVariable long page, @PathVariable long size,
                       @RequestParam(required = false) String begin,
                       @RequestParam(required = false) String end) {
        return examsService.pageExams(page,size,begin,end);
    }

//    添加考试信息
    @ApiOperation(value = "添加考试信息")
    @PostMapping("addExams")
    public R addExams(@RequestBody Exams exams) {
        boolean flag = examsService.save(exams);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

//    修改考试信息
    @ApiOperation(value = "修改考试信息")
    @PostMapping("updateExams")
    public R updateExams(@RequestBody Exams exams) {
        if (StringUtils.isEmpty(exams)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean update = examsService.updateById(exams);
        if(update) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

//    逻辑删除考试信息
    @ApiOperation(value = "逻辑删除考试信息")
    @PostMapping("removeExams/{id}")
    public R removeExams(@PathVariable String id){
        boolean remove = examsService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

//根据id查询考试信息
    @ApiOperation(value = "根据id查询考试信息")
    @GetMapping("getExams/{id}")
    public R getExamById(@PathVariable String id) {
        Exams byId = examsService.getById(id);
        return R.ok().data("item", byId);
    }

//    获取所有考试信息
    @ApiOperation(value = "获取所有考试信息")
    @GetMapping("findAllExams")
    public R findAllTeacher() {
        List<Exams> content = examsService.findAllExams();
        return R.ok().data("content", content);
    }
}

